<?php
/**
 * 微信启动文件
 * User: Acer
 * Date: 2022/7/9
 * Time: 12:40
 */
namespace app\wx\controller;
use app\core\MyController;
use think\Log;

define("TOKEN", "yxxdjk20220709");    //公众号平台上自定义TOKEN
class WxConfig extends MyController
{
    protected $noNeedAction = ['*']; // 不需要校验控制器，通道
    // 操作前置
    protected $beforeActionList = ['my_initialize'];

    // 操作前置方法
    public function my_initialize()
    {

    }

    public function getConfig(){
        Log::record("============= 进入 ===============");
        // 触发消息发送机制
        $WxSendMsg = new WxSendMsg();
        $WxSendMsg->first_follow_gzh();
        if (!isset($_GET['echostr'])) {
            echo 111;//某个方法，后续补上
        }else{
            $this->valid();    //调用valid函数进行基本配置
        }
    }

        private function valid(){    //用于基本配置的函数
            $echoStr = $_GET["echostr"];
            if ($this->checkSignature()) {
                echo $echoStr;
                exit;
            }
        }


        private function checkSignature()
        {
            $signature = $_GET["signature"]; //这个signature其实就是在微信公众平台已经加密好的字符串
            $timestamp = $_GET["timestamp"]; //timestamp其实就是一个时间戳
            $nonce = $_GET["nonce"];         //nonce是一个随机参数
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
            Log::info("============= 校验 ===============");
            if ($tmpStr == $signature) {
                return true;
            } else {
                return false;
            }
        }
}